var savedRangeEditable,isInFocusEditable;
function saveSelectionEditable()
{
    if(window.getSelection)//non IE Browsers
    {
        savedRangeEditable = window.getSelection().getRangeAt(0);
    }
    else if(document.selection)//IE
    { 
        savedRangeEditable = document.selection.createRange();  
    } 
}

function restoreSelectionEditable()
{
    isInFocusEditable = true;
    currentEditable.focus();
    if (savedRangeEditable != null) {
        if (window.getSelection)//non IE and there is already a selection
        {
            var s = window.getSelection();
            if (s.rangeCount > 0) 
                s.removeAllRanges();
            s.addRange(savedRangeEditable);
        }
        else 
            if (document.createRange)//non IE and no selection
            {
                window.getSelection().addRange(savedRangeEditable);
            }
            else 
                if (document.selection)//IE
                {
                    savedRangeEditable.select();
                }
    }
}
//this part onwards is only needed if you want to restore selection onclick
var isInFocusEditable = false;
function onDivBlurEditable()
{
    isInFocusEditable = false;
}

function cancelEventEditable(e)
{
    if (isInFocusEditable == false && savedRangeEditable != null) {
        if (e && e.preventDefault) {
            //alert("FF");
            e.stopPropagation(); // DOM style (return false doesn't always work in FF)
            e.preventDefault();
        }
        else {
            window.event.cancelBubble = true;//IE stopPropagation
        }
        restoreSelectionEditable();
        return false; // false = IE style
    }
}

var currentEditable = null;
function loadEditableExtend()
{
	var $temp = $("[contenteditable=true]");
	if ($temp.length > 0)
	{
		currentEditable = $temp[0];
		
		$temp.focus(function(){
			restoreSelectionEditable();
		}).blur(function(){
			onDivBlurEditable();
		}).click(function(e){
			return cancelEventEditable(e);
		}).mousedown(function(e){
			return cancelEventEditable(e);
		}).mouseup(function(){
			saveSelectionEditable();
		}).keyup(function(){
			saveSelectionEditable();
		});
	}
	//setFocusEditable();
	//var btnTemp = $("body").append("<input type='button' value='' onclick='setFocusEditable()' id='btnGainFocusEditor'/>");
	//$("#btnGainFocusEditor").click();
}

function setFocusEditable()
{
	if (currentEditable)
	{
		currentEditable.focus();
	}
}


var isMouseHoverBoard = false;
function addConfigPopupEditor()
{
	$(".nicEdit-popup-menu-editor").hover(function () {
        isMouseHoverBoard = true;
    }, function () {
        isMouseHoverBoard = false;
    });
}

$("body").mouseup(function () {
        if (!isMouseHoverBoard) 
		{
			if ($(".nicEdit-popup-menu-editor").length > 0)
			{
				$(".nicEdit-popup-menu-editor").remove();
			}
		}
    }).keyup(function (e) {
        if (!e) {
            e = window.event;
        }
        var keyCode = e.keyCode;
        if (keyCode == 27) {
            if ($(".nicEdit-popup-menu-editor").length > 0)
			{
				$(".nicEdit-popup-menu-editor").remove();
			}
        }
    });

